package com.ljkj.zjf.rest;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ljkj.access.Tuser;
import com.ljkj.common.application.SpringApplicationContext;
import com.ljkj.common.db.domain.ZjfGood;
import com.ljkj.common.db.util.JSONUTIL;
import com.ljkj.zjf.common.CacheManager;
import com.ljkj.zjf.common.GlobalNames;
import com.ljkj.zjf.service.TuserService;
import com.ljkj.zjf.service.ZjfGoodService;

@Path("rest/good")
public class ZjfGoodSource {
	
	private static ZjfGoodService zjfGoodService;
	private static TuserService tuserService;
	
	//初始化service
	static {
		zjfGoodService  = (ZjfGoodService) SpringApplicationContext.getBean("zjfGoodService");
		tuserService =  (TuserService) SpringApplicationContext.getBean("tuserService");
	}
	
	private static Logger logger = LoggerFactory.getLogger(ZjfGoodSource.class);
	
	@Path("/list")
	@POST
	@Produces("application/json; charset=utf-8")
	public String goodsClassList(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		String userId = json_object.getString("userId"); 
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String matchRes = CacheManager.getSession().getIfPresent(userId);
		if (null == matchRes || "".equals(matchRes)) {
			errorMap.put("errMsg", "您尚未登录，请登录再操作。");
			return JSON.toJSONString(errorMap);
		}
		
		String params = json_object.getString("params");
		String pageIndex = json_object.getString("pageIndex");
		int currentPage = 1;
		if(pageIndex != null && !"".equals(pageIndex)){
			currentPage = Integer.parseInt(pageIndex)+1;
		}
		String pageSize_str = json_object.getString("pageSize");
		int pageSize = 10;
		if(pageSize_str != null && !"".equals(pageSize_str)){
			pageSize = Integer.parseInt(pageSize_str);
		}
		String sortField = json_object.getString("sortField");
		String sortOrder = json_object.getString("sortOrder");
		
		String result = this.zjfGoodService.getGoodsByProperties(params, currentPage, pageSize, sortField, sortOrder);
		return result;
		
	}
	
	
	@Path("/info")
	@POST
	@Produces("application/json; charset=utf-8")
	public String addOrEditGoodInfo(String json_para) throws Exception {
		
		String decode_str = URLDecoder.decode(json_para, "utf-8");
		decode_str = JSONUTIL.JsonFromString(decode_str);
		JSONObject json_object = JSON.parseObject(decode_str);
		
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("errCode", GlobalNames.CALL_RESULT_FAIL);
		String userId = json_object.getString("userId"); 
		if (userId == null || "".equals(userId)) {
			errorMap.put("errMsg", "用户名不能为空");
			return JSON.toJSONString(errorMap);
		}
		
		String goodsId = json_object.getString("goodsId");
		ZjfGood zjfGood = null;
		if(goodsId != null && !"".equals(goodsId)){
			zjfGood = this.zjfGoodService.getGoodById(goodsId);
		}
		if(zjfGood == null){
			zjfGood = new ZjfGood();
		}
		
		String goodsName = json_object.getString("goodsName");
		if(goodsName!= null){
			zjfGood.setGoodsName(goodsName);
		}
		
		String goodsDes = json_object.getString("goodsDes");
		if(goodsDes != null && !"".equals(goodsDes)){
			zjfGood.setGoodsDes(goodsDes);
		}
		
		String goodsPrice_temp = json_object.getString("goodsPrice");
		BigDecimal goodsPrice;
		if(goodsPrice_temp != null && !"".equals(goodsPrice_temp)){
			goodsPrice = new BigDecimal(goodsPrice_temp);
			zjfGood.setGoodsPrice(goodsPrice);
		}
		
		String goodsStorePrice_temp = json_object.getString("goodsStorePrice");
		BigDecimal goodsStorePrice;
		if(goodsStorePrice_temp != null && !"".equals(goodsStorePrice_temp)){
			goodsStorePrice = new BigDecimal(goodsStorePrice_temp);
			zjfGood.setGoodsStorePrice(goodsStorePrice);
		}
		
		
		String goodsInventory_temp = json_object.getString("goodsInventory");
		Integer goodsInventory = null;
		if(goodsInventory_temp != null && !"".equals(goodsInventory_temp)){
			goodsInventory = Integer.parseInt(goodsInventory_temp);
			zjfGood.setGoodsInventory(goodsInventory);
		}
		
		String goodsDetails = json_object.getString("goodsDetails");
		if(goodsDetails != null && !"".equals(goodsDetails)){
			zjfGood.setGoodsDetails(goodsDetails);
		}
		
		String goodsPackDetails = json_object.getString("goodsPackDetails");
		if(goodsPackDetails != null && !"".equals(goodsPackDetails)){
			zjfGood.setGoodsPackDetails(goodsPackDetails);
		}
		
		String goodsSerial = json_object.getString("goodsSerial");
		if(goodsSerial != null && !"".equals(goodsSerial)){
			zjfGood.setGoodsSerial(goodsSerial);
		}
		
		String goodsService = json_object.getString("goodsService");
		if(goodsService != null && !"".equals(goodsService)){
			zjfGood.setGoodsService(goodsService);
		}
		
		String goodsRecommend_temp = json_object.getString("goodsRecommend");
		Integer goodsRecommend = null;
		if(goodsRecommend_temp != null && !"".equals(goodsRecommend_temp)){
			goodsRecommend = Integer.parseInt(goodsRecommend_temp);
			zjfGood.setGoodsRecommend(goodsRecommend);
		}
		
		String goodsCod_temp = json_object.getString("goodsCod");
		Integer goodsCod = null;
		if(goodsCod_temp != null && !"".equals(goodsCod_temp)){
			goodsCod = Integer.parseInt(goodsCod_temp);
			zjfGood.setGoodsCod(goodsCod);
		}
		
		String goodsStatus_temp = json_object.getString("goodsStatus");
		Integer goodsStatus = null;
		if(goodsStatus_temp != null && !"".equals(goodsStatus_temp)){
			goodsStatus = Integer.parseInt(goodsStatus_temp);
			zjfGood.setGoodsStatus(goodsStatus);
		}
		
		String goodsShelvesTime = json_object.getString("goodsShelvesTime");
		if(goodsShelvesTime != null && !"".equals(goodsShelvesTime)){
			zjfGood.setGoodsShelvesTime(goodsShelvesTime);
		}
		
		Integer goodsTransfee = null;
		String goodsTransfee_temp = json_object.getString("goodsTransfee");
		if(goodsTransfee_temp != null && !"".equals(goodsTransfee_temp)){
			goodsTransfee = Integer.parseInt(goodsTransfee_temp);
			zjfGood.setGoodsTransfee(goodsTransfee);
		}
		
		String goodsMainPhoto = json_object.getString("goodsMainPhoto");
		if(goodsMainPhoto != null && !"".equals(goodsMainPhoto)){
			zjfGood.setGoodsMainPhoto(goodsMainPhoto);
		}
		
		String goodsDesImg = json_object.getString("goodsDesImg");
		if(goodsDesImg != null && !"".equals(goodsDesImg)){
			zjfGood.setGoodsDesImg(goodsDesImg);
		}
		
		Integer goodsActivityStatus = null;
		String goodsActivityStatus_temp = json_object.getString("goodsActivityStatus");
		if(goodsActivityStatus_temp != null && !"".equals(goodsActivityStatus_temp)){
			goodsActivityStatus = Integer.parseInt(goodsActivityStatus_temp);
			zjfGood.setGoodsActivityStatus(goodsActivityStatus);
		}
		
		String goodsActivityId = json_object.getString("goodsActivityId");
		if(goodsActivityId != null && !"".equals(goodsActivityId)){
			zjfGood.setGoodsActivityId(goodsActivityId);
		}
		
		Integer goodsCliffordStatus = null;
		String goodsCliffordStatus_temp = json_object.getString("goodsCliffordStatus");
		if(goodsCliffordStatus_temp != null && !"".equals(goodsCliffordStatus_temp)){
			goodsCliffordStatus = Integer.parseInt(goodsCliffordStatus_temp);
			zjfGood.setGoodsCliffordStatus(goodsCliffordStatus);
		}
		
		String goodsClassId = json_object.getString("goodsClassId");
		if(goodsClassId != null && !"".equals(goodsClassId)){
			zjfGood.setGoodsClassId(goodsClassId);
		}
		
		Integer disabledState = null;
		String disabledState_temp = json_object.getString("disabledState");
		if(disabledState_temp != null && !"".equals(disabledState_temp)){
			disabledState = Integer.parseInt(disabledState_temp);
			zjfGood.setDisabledState(disabledState);
		}
		
		Tuser tuser = this.tuserService.getUserById(Integer.valueOf(userId));
		
		String result = this.zjfGoodService.addOrEditGoodInfo(zjfGood, tuser);
		return result;
		
	}


}
